package com.film.admincontroller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.film.domain.FilmType;
import com.film.service.FilmTypeService;
import com.film.service.impl.FilmTypeServiceImpl;
@WebServlet("/addType.do")
public class AddFilmTypeController extends HttpServlet{
	private FilmTypeService filmTypeService = new FilmTypeServiceImpl();
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String typeName = request.getParameter("typeName");
		//查询所有电影类型
		List<FilmType> types = filmTypeService.selectAllFilmType();
		//如果已有当前类型则不能添加成功
		boolean flag = true;
		int result = 0;
		for (FilmType filmType : types) {
			String type = filmType.getTypeName();
			if(type.equals(typeName)) {
				flag = false;
				break;
			}
		}
		if(flag) {
			result = filmTypeService.addTypeName(typeName);
		}
		if(result != 1) {
			HttpSession session = request.getSession();
			session.setAttribute("addTypeResultMsg", "当前类型已存在了喔");
		}
		response.sendRedirect("/film/filmType.do");
	}
}
